package homework;

import java.util.Comparator;
import java.util.TreeSet;

/*
4. 分别用Comparable和Comparator两个接口对下列四位同学的成绩做降序排序，
如果成绩一样，那在成绩排序的基础上按照年龄由小到大排序。
姓名（String）	年龄（int）	分数（float）
liusan	              20	90.0F
lisi		      22	90.0F
wangwu		      20	99.0F
sunliu		      22	100.0F
*/
public class Demo3 {
    public static void main(String[] args) {
        TreeSet<Student>list = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {

                float result = o2.getGrade() - o1.getGrade();
                result =result == 0? o1.getAge() - o2.getAge(): result;
                result =result == 0? o1.getName().compareTo(o2.getName()) : result;
                return (int) result;
/*                if(o1.getGrade()==o2.getGrade()){
                    return o1.getAge()-o2.getAge();
                }
                return (int)(o2.getGrade()-o1.getGrade());*/
            }
        });
        Student s1 = new Student("liusan",20,90.0F);
        Student s2 = new Student("lisi",22,90.0F);
        Student s3 = new Student("wangwu",20,99.0F);
        Student s4 = new Student("sunliu",22,100.0F);
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        for (Student student : list) {
            System.out.print(student+" ");
        }
        System.out.println();
    }
}
